<?php
namespace Mob\Model;

use Think\Model;

/**签到参数模型
 * Class SignParamModel
 */
class SignParamModel extends Model
{
	 protected $tableName = 'sign_param';

	 public function creatorParam($schooId,$collegeId,$signStartTime,$signEndTime,$isWeekend='false',$exit)
	 {
	  $data['schooId']=$schooId;
	  $data['collegeId']=$collegeId;
	  $data['signStartTime']=$signStartTime;
	  $data['signEndTime']=$signEndTime;
	  $data['isWeekend']=$isWeekend;
	  $data['status']=1;
	  $data['exit']=$exit;
	  return $this->add($data);

	 }
	 public function updateParam($schooId,$collegeId,$data,$memcache=true)
	 {
	 $is_exist = $this->is_existSetTing($schooId,$collegeId);
	 if($is_exist)
	 {
	 cache_del("signParam_$schooId"."_$collegeId");
	  $this->where(array('schooId'=>$schooId,'collegeId'=>$collegeId,'status'=>1))->save($data);
	  return true;

	 }else
	 {

	 $signStartTime= $data['signStartTime'];
	 $signEndTime=  $data['signEndTime'];
	 $isWeekend= $data['isWeekend'];
	 $exit= $data['exit'];

	 return  $this->creatorParam($schooId,$collegeId,$signStartTime,$signEndTime,$isWeekend,$exit);
	 }




	 }
	 public function get_param($schooId,$collegeId,$memcached=true)
	 {
      if($memcached)
      {
	  $res = cache_get("signParam_$schooId"."_$collegeId");
	  if($res)
	  {
	  return $res;
	  }
      }
      $where = array('schooId'=>$schooId,'collegeId'=>$collegeId);
      $res =  $this->where($where)->field('schooId,collegeId,signStartTime,signEndTime,status,isWeekend,exit')->find();
      if(empty($res)) //如果返回结果为空 ,,则默认配置
      {
      $res = array('schooId'=>$schooId,'collegeId'=>$collegeId,'signStartTime'=>'21:00:00','signEndTime'=>'23:00:00','status'=>1,'isWeekend'=>false,'exit'=>null);
      }
      if($memcached)
      {
      cache_set("signParam_$schooId"."_$collegeId",$res);
      }
      return $res;

	 }
	 public function is_existSetTing($schooId,$collegeId)
	 {
	 	 $where = array('schooId'=>$schooId,'collegeId'=>$collegeId);
      return  $this->where($where)->field('schooId,collegeId,signStartTime,signEndTime,status,isWeekend,exit')->find();

	 }











}